<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户管理系统</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 20px;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            background: white;
            border-radius: 15px;
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
            overflow: hidden;
        }

        .header {
            background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
            color: white;
            padding: 30px;
            text-align: center;
        }

        .header h1 {
            font-size: 2.5em;
            margin-bottom: 10px;
            font-weight: 300;
        }

        .header p {
            font-size: 1.1em;
            opacity: 0.9;
        }

        .content {
            padding: 30px;
        }

        .form-section {
            background: #f8f9fa;
            padding: 25px;
            border-radius: 10px;
            margin-bottom: 30px;
            border: 1px solid #e9ecef;
        }

        .form-section h2 {
            color: #495057;
            margin-bottom: 20px;
            font-size: 1.5em;
            border-bottom: 2px solid #4facfe;
            padding-bottom: 10px;
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            color: #495057;
            font-weight: 500;
        }

        .form-group input {
            width: 100%;
            padding: 12px 15px;
            border: 2px solid #e9ecef;
            border-radius: 8px;
            font-size: 16px;
            transition: border-color 0.3s ease;
        }

        .form-group input:focus {
            outline: none;
            border-color: #4facfe;
            box-shadow: 0 0 0 3px rgba(79, 172, 254, 0.1);
        }

        .btn {
            padding: 12px 25px;
            border: none;
            border-radius: 8px;
            font-size: 16px;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.3s ease;
            margin-right: 10px;
            margin-bottom: 10px;
        }

        .btn-primary {
            background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
            color: white;
        }

        .btn-primary:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(79, 172, 254, 0.4);
        }

        .btn-success {
            background: linear-gradient(135deg, #43e97b 0%, #38f9d7 100%);
            color: white;
        }

        .btn-success:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(67, 233, 123, 0.4);
        }

        .btn-warning {
            background: linear-gradient(135deg, #fa709a 0%, #fee140 100%);
            color: white;
        }

        .btn-warning:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(250, 112, 154, 0.4);
        }

        .btn-danger {
            background: linear-gradient(135deg, #ff6b6b 0%, #ee5a24 100%);
            color: white;
        }

        .btn-danger:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(255, 107, 107, 0.4);
        }

        .table-container {
            background: white;
            border-radius: 10px;
            overflow: hidden;
            box-shadow: 0 5px 15px rgba(0,0,0,0.08);
        }

        .table-header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 20px;
            text-align: center;
        }

        .table-header h2 {
            margin: 0;
            font-size: 1.8em;
            font-weight: 300;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            background: white;
        }

        th, td {
            padding: 15px;
            text-align: left;
            border-bottom: 1px solid #e9ecef;
        }

        th {
            background: #f8f9fa;
            color: #495057;
            font-weight: 600;
            font-size: 14px;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }

        tr:hover {
            background: #f8f9fa;
        }

        .status {
            padding: 5px 10px;
            border-radius: 20px;
            font-size: 12px;
            font-weight: 500;
            text-transform: uppercase;
        }

        .status.success {
            background: #d4edda;
            color: #155724;
        }

        .status.error {
            background: #f8d7da;
            color: #721c24;
        }

        .loading {
            text-align: center;
            padding: 20px;
            color: #6c757d;
        }

        .error-message {
            background: #f8d7da;
            color: #721c24;
            padding: 15px;
            border-radius: 8px;
            margin-bottom: 20px;
            border: 1px solid #f5c6cb;
        }

        .success-message {
            background: #d4edda;
            color: #155724;
            padding: 15px;
            border-radius: 8px;
            margin-bottom: 20px;
            border: 1px solid #c3e6cb;
        }

        .search-section {
            margin-bottom: 20px;
        }

        .search-input {
            width: 300px;
            padding: 10px 15px;
            border: 2px solid #e9ecef;
            border-radius: 8px;
            font-size: 14px;
            margin-right: 10px;
        }

        .search-input:focus {
            outline: none;
            border-color: #4facfe;
        }

        @media (max-width: 768px) {
            .container {
                margin: 10px;
                border-radius: 10px;
            }

            .header {
                padding: 20px;
            }

            .header h1 {
                font-size: 2em;
            }

            .content {
                padding: 20px;
            }

            .btn {
                width: 100%;
                margin-bottom: 10px;
            }

            .search-input {
                width: 100%;
                margin-bottom: 10px;
            }

            table {
                font-size: 14px;
            }

            th, td {
                padding: 10px 8px;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>用户管理系统</h1>
            <p>FastAPI 用户管理界面</p>
        </div>

        <div class="content">
            <!-- 用户注册表单 -->
            <div class="form-section">
                <h2>用户注册</h2>
                <div class="form-group">
                    <label for="username">用户名:</label>
                    <input type="text" id="username" placeholder="请输入用户名">
                </div>
                <div class="form-group">
                    <label for="phone">手机号:</label>
                    <input type="tel" id="phone" placeholder="请输入手机号">
                </div>
                <div class="form-group">
                    <label for="email">邮箱:</label>
                    <input type="email" id="email" placeholder="请输入邮箱">
                </div>
                <div class="form-group">
                    <label for="password">密码:</label>
                    <input type="password" id="password" placeholder="请输入密码">
                </div>
                <button class="btn btn-primary" onclick="registerUser()">注册用户</button>
            </div>

            <!-- 用户登录表单 -->
            <div class="form-section">
                <h2>用户登录</h2>
                <div class="form-group">
                    <label for="loginUsername">用户名:</label>
                    <input type="text" id="loginUsername" placeholder="请输入用户名">
                </div>
                <div class="form-group">
                    <label for="loginPassword">密码:</label>
                    <input type="password" id="loginPassword" placeholder="请输入密码">
                </div>
                <button class="btn btn-success" onclick="loginUser()">登录</button>
            </div>

            <!-- 用户管理 -->
            <div class="form-section">
                <h2>用户管理</h2>
                <div class="form-group">
                    <label for="userId">用户ID:</label>
                    <input type="number" id="userId" placeholder="请输入用户ID">
                </div>
                <div class="form-group">
                    <label for="newPhone">新手机号:</label>
                    <input type="tel" id="newPhone" placeholder="请输入新手机号">
                </div>
                <div class="form-group">
                    <label for="newEmail">新邮箱:</label>
                    <input type="email" id="newEmail" placeholder="请输入新邮箱">
                </div>
                <button class="btn btn-warning" onclick="updateUser()">更新用户</button>
                <button class="btn btn-danger" onclick="deleteUser()">删除用户</button>
            </div>

            <!-- 搜索和用户列表 -->
            <div class="table-container">
                <div class="table-header">
                    <h2>用户列表</h2>
                </div>
                <div style="padding: 20px;">
                    <div class="search-section">
                        <input type="text" class="search-input" id="searchInput" placeholder="搜索用户...">
                        <button class="btn btn-primary" onclick="searchUsers()">搜索</button>
                        <button class="btn btn-success" onclick="loadUsers()">刷新列表</button>
                    </div>
                    <div id="message"></div>
                    <div id="userTable"></div>
                </div>
            </div>
        </div>
    </div>

    <script>
        const API_BASE = 'http://localhost:8000/api';

        // 显示消息
        function showMessage(message, type = 'success') {
            const messageDiv = document.getElementById('message');
            messageDiv.innerHTML = `<div class="${type}-message">${message}</div>`;
            setTimeout(() => {
                messageDiv.innerHTML = '';
            }, 5000);
        }

        // 用户注册
        async function registerUser() {
            const username = document.getElementById('username').value;
            const phone = document.getElementById('phone').value;
            const email = document.getElementById('email').value;
            const password = document.getElementById('password').value;

            if (!username || !phone || !email || !password) {
                showMessage('请填写所有字段', 'error');
                return;
            }

            try {
                const response = await fetch(`${API_BASE}/users/register`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        username,
                        phone,
                        email,
                        password
                    })
                });

                const data = await response.json();

                if (response.ok) {
                    showMessage('用户注册成功！');
                    clearForm('register');
                    loadUsers();
                } else {
                    showMessage(`注册失败: ${data.detail}`, 'error');
                }
            } catch (error) {
                showMessage(`注册失败: ${error.message}`, 'error');
            }
        }

        // 用户登录
        async function loginUser() {
            const username = document.getElementById('loginUsername').value;
            const password = document.getElementById('loginPassword').value;

            if (!username || !password) {
                showMessage('请填写用户名和密码', 'error');
                return;
            }

            try {
                const response = await fetch(`${API_BASE}/users/login`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        username,
                        password
                    })
                });

                const data = await response.json();

                if (response.ok) {
                    showMessage(`登录成功！欢迎 ${data.username}`);
                    clearForm('login');
                } else {
                    showMessage(`登录失败: ${data.detail}`, 'error');
                }
            } catch (error) {
                showMessage(`登录失败: ${error.message}`, 'error');
            }
        }

        // 更新用户
        async function updateUser() {
            const userId = document.getElementById('userId').value;
            const newPhone = document.getElementById('newPhone').value;
            const newEmail = document.getElementById('newEmail').value;

            if (!userId) {
                showMessage('请输入用户ID', 'error');
                return;
            }

            if (!newPhone && !newEmail) {
                showMessage('请至少输入一个新手机号或新邮箱', 'error');
                return;
            }

            try {
                const params = new URLSearchParams();
                if (newPhone) params.append('phone', newPhone);
                if (newEmail) params.append('email', newEmail);

                const response = await fetch(`${API_BASE}/users/${userId}?${params}`, {
                    method: 'PUT'
                });

                const data = await response.json();

                if (response.ok) {
                    showMessage('用户更新成功！');
                    clearForm('update');
                    loadUsers();
                } else {
                    showMessage(`更新失败: ${data.detail}`, 'error');
                }
            } catch (error) {
                showMessage(`更新失败: ${error.message}`, 'error');
            }
        }

        // 删除用户
        async function deleteUser() {
            const userId = document.getElementById('userId').value;

            if (!userId) {
                showMessage('请输入用户ID', 'error');
                return;
            }

            if (!confirm('确定要删除这个用户吗？')) {
                return;
            }

            try {
                const response = await fetch(`${API_BASE}/users/${userId}`, {
                    method: 'DELETE'
                });

                if (response.ok) {
                    showMessage('用户删除成功！');
                    clearForm('update');
                    loadUsers();
                } else {
                    const data = await response.json();
                    showMessage(`删除失败: ${data.detail}`, 'error');
                }
            } catch (error) {
                showMessage(`删除失败: ${error.message}`, 'error');
            }
        }

        // 加载用户列表
        async function loadUsers() {
            const tableDiv = document.getElementById('userTable');
            tableDiv.innerHTML = '<div class="loading">加载中...</div>';

            try {
                const response = await fetch(`${API_BASE}/users/`);
                const users = await response.json();

                if (response.ok) {
                    displayUsers(users);
                } else {
                    showMessage(`加载用户列表失败: ${users.detail}`, 'error');
                }
            } catch (error) {
                showMessage(`加载用户列表失败: ${error.message}`, 'error');
            }
        }

        // 搜索用户
        async function searchUsers() {
            const searchTerm = document.getElementById('searchInput').value.toLowerCase();
            const tableDiv = document.getElementById('userTable');
            
            if (!searchTerm) {
                loadUsers();
                return;
            }

            tableDiv.innerHTML = '<div class="loading">搜索中...</div>';

            try {
                const response = await fetch(`${API_BASE}/users/`);
                const users = await response.json();

                if (response.ok) {
                    const filteredUsers = users.filter(user => 
                        user.username.toLowerCase().includes(searchTerm) ||
                        user.phone.includes(searchTerm) ||
                        user.email.toLowerCase().includes(searchTerm)
                    );
                    displayUsers(filteredUsers);
                } else {
                    showMessage(`搜索失败: ${users.detail}`, 'error');
                }
            } catch (error) {
                showMessage(`搜索失败: ${error.message}`, 'error');
            }
        }

        // 显示用户列表
        function displayUsers(users) {
            const tableDiv = document.getElementById('userTable');
            
            if (users.length === 0) {
                tableDiv.innerHTML = '<div class="loading">没有找到用户</div>';
                return;
            }

            let tableHTML = `
                <table>
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>用户名</th>
                            <th>手机号</th>
                            <th>邮箱</th>
                            <th>注册时间</th>
                        </tr>
                    </thead>
                    <tbody>
            `;

            users.forEach(user => {
                const createdDate = user.created_at ? new Date(user.created_at).toLocaleString('zh-CN') : '未知';
                tableHTML += `
                    <tr>
                        <td>${user.id}</td>
                        <td>${user.username}</td>
                        <td>${user.phone}</td>
                        <td>${user.email}</td>
                        <td>${createdDate}</td>
                    </tr>
                `;
            });

            tableHTML += '</tbody></table>';
            tableDiv.innerHTML = tableHTML;
        }

        // 清空表单
        function clearForm(type) {
            if (type === 'register') {
                document.getElementById('username').value = '';
                document.getElementById('phone').value = '';
                document.getElementById('email').value = '';
                document.getElementById('password').value = '';
            } else if (type === 'login') {
                document.getElementById('loginUsername').value = '';
                document.getElementById('loginPassword').value = '';
            } else if (type === 'update') {
                document.getElementById('userId').value = '';
                document.getElementById('newPhone').value = '';
                document.getElementById('newEmail').value = '';
            }
        }

        // 页面加载时获取用户列表
        window.onload = function() {
            loadUsers();
        };

        // 搜索框回车事件
        document.getElementById('searchInput').addEventListener('keypress', function(e) {
            if (e.key === 'Enter') {
                searchUsers();
            }
        });
    </script>
</body>
</html> 